﻿//---------ChoseVAT--------------*@
    $(document).ready(function() {
        var content = $('#kodEDRPOY').val();
            if (content.length > 0) {
                var url = "/AjaxQuery/GetFirmName?code=" + content;
                $.get(url, function (content) {
                    $("#nazva").val(content);
                });

                var url = "/AjaxQuery/GetFirmAddress?code=" + content;
                $.get(url, function (content) {
                    $("#adresa").val(content);
                });
                }
});
//---------NumberOnly-------------*@
  $(function() {
    $(".number").keydown(function(event) {
         // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 ) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            } 
        }
    });
//---------PostqueryForVatCode-------------*@
    $('#kodEDRPOY').keyup(function() { 
        var content = $('#kodEDRPOY').val();
            if (content.length > 0) {
                var url = "/AjaxQuery/GetFirmName?code=" + content;
                $.get(url, function (content) {
                    $("#nazva").val(content);
                });

                var url = "/AjaxQuery/GetFirmAddress?code=" + content;
                $.get(url, function (content) {
                    $("#adresa").val(content);
                });

            }
    });
    ////JF Code
        $('#kodedrpoy2').keyup(function() { 
        var content = $('#kodedrpoy2').val();
            if (content.length == 8) {
             $.getJSON("/AjaxQuery/SearchJuristicPerson",
                      {
                        code: content
                      },
                      function(data) {
                           $("#kodedrpoy2").val(data.Code);
                           $("#povnanazva").val(data.FullName);
                           $("#index").val(data.PostIndex);
                           $("#oblast").val(data.Region);
                           $("#rayon").val(data.District);
                           $("#mssmt").val(data.City);
                           $("#vul").val(data.Street);
                           $("#bud").val(data.Build);
                           $("#kv").val(data.Apartment);
                           $("#tel").val(data.PhoneNumber);
                           $("#percent").val(data.ShareStateProperty);
                   
                           $("#investmentFund").attr("checked",data.IsInvestmentFund)
                           $("#sumaktyviv").val(data.TotalAssets);
                           $("#vartaktyviv").val(data.CostAcquisition);
                           $("#prozgolosivvid").val(data.CostAcquisitionInPercent);
                        });
            }
    });
     $("#kodedrpoy2").keydown(function (event) {
         // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 ) {
                           $("#povnanazva").val('');
                           $("#index").val('');
                           $("#oblast").val('');
                           $("#rayon").val('');
                           $("#mssmt").val('');
                           $("#vul").val('');
                           $("#bud").val('');
                           $("#kv").val('');
                           $("#tel").val('');
                           $("#percent").val('');
                           $("#investmentFund").attr("checked",false);
                           $("#sumaktyviv").val('');
                           $("#vartaktyviv").val('');
                           $("#prozgolosivvid").val('');
        }
        });
});
//---------DatePicker-------------*@
  $(function () {
            $(".datedoc").datepicker({
                 defaultDate: '01-01-1985',
                 changeYear: true,
                  yearRange: '1920:'+new Date(),
            });
        });
//--------AutoCompleter-----------*@
  $(function() {
     //Region
     $(".region").autocomplete({
     source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchRegions', type: "POST", dataType: "json",
                    data: {
                        term: request.term
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdnreg").val(ui.item.id);
            }
});

     //District
     $(".district").autocomplete({
     source: function(request, response) {
                $.ajax({
                    url:  '/AjaxQuery/SearchDistricts', type: "POST", dataType: "json",
                    data: {
                        term: request.term,
						regionID:   $("#hdnreg").val()
					} ,
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                               value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdndist").val(ui.item.id);
            }
});
 
    //City
	 $(".city").autocomplete({
	source: function( request, response ) {
				$.ajax({
					  url:  '/AjaxQuery/SearchCities', type: "POST", dataType: "json",
					data: {
                        term: request.term,
						districtID:   $("#hdndist").val()
					} ,
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
     select: function(event, ui) {
               $("#hdncity").val(ui.item.id);

            }
		});

     //Street
	 $(".street").autocomplete({
	 source: function( request, response ) {
				$.ajax({
					  url:  '/AjaxQuery/SearchStreets', type: "POST", dataType: "json",
					data: {
                        term: request.term,
						cityID:   $("#hdncity").val()
					},
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                value: item.Name, id: item.ID
                            };
                        }));
                    }
                });
            },
            minLength: 2
		});
});